// pages/delivery-center/index.js
const {
  $Toast
} = require('../../components/dist/base/index');
import config from '../../etc/config'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    deId: '',
    showModalStatus: false,
    orderList: [],
    height: 300,
    deliveryById: {},
    canDrawMoney:''
  },
  powerDrawer: function(e) {
    var currentStatu = e.currentTarget.dataset.status;
    this.util()
  },
  util: function() {
    /* 动画部分 */
    // 第1步：创建动画实例
    var animation = wx.createAnimation({
      duration: 200, //动画时长
      timingFunction: "linear", //线性
      delay: 0 //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(this.data.height * 0.6).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function() {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        animationData: animation
      })
      //关闭抽屉
        this.setData({
          showModalStatus: false
        });
        this.setData({
          disableScroll: true,
          showModalStatus: false 
        })
      // this.handlerOpenRobberyOrder()
    }.bind(this), 500)
  },
  // 打开接单中心
  handlerOpenRobberyOrder() {
    this.getOrderList()
  },
  getUserInfo() {
    let params = {
      userId: wx.getStorageSync('userId')
    }
    let token = wx.getStorageSync('token')
    config._post('/user/getUserInfo', token, params).then(res => {
      console.log(res)
      if (res.data) {
        // 用户基本信息展示
        this.setData({
          userInfo: res.data,
          // version: app.globalData.version
        })
      }
    })
  },
    cancelCs(e) {
    let webview = e.target.dataset.webview
    wx.navigateTo({
      url: '../kkk/index?webview=' + encodeURIComponent(webview),
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
    },
  // 接单
  handlerRobberyOrder(e) {
    let order = e.currentTarget.dataset.order
    let params = {
      deliveryId: this.data.deId,
      orderId: order.orderId
    }
    let token = wx.getStorageSync('token')
    config._post('/deliverymanOrder/grabOrder', token, params).then(res => {
      console.log(res)
      if (res.code == 0) {
        this.getOrderList()
        $Toast({
          content: '接单成功'
        });
      }else {
        $Toast({
          content: res.msg
        });
        this.getOrderList()
      }
      // if (res.data) {
      //   this.setData({
      //     orderList: res.data,
      //     showModalStatus: true
      //   })
      //   console.log('orderList::', this.data.orderList)
      // }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getOrderList() {
    let params = {
      deliveryId: this.data.deId
    }
    let token = wx.getStorageSync('token')
    config._post('/deliverymanOrder/availableOrders', token, params).then(res => {
      if (res.code== 0) {
          this.setData({
            orderList: res.data,
            showModalStatus: true
          })
        if (!this.data.orderList || this.data.orderList<=0){
          $Toast({
            content: '暂无可抢订单！'
          });
          this.setData({
            showModalStatus: false
          })
        }
      }else {
        $Toast({
          content: masg
        });
      }
    })
  },
  onLoad: function(options) {
    wx.hideShareMenu()
    let that = this;
    wx.getSystemInfo({
      success: function(res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        that.setData({
          height: height
        });
      }
    });
    this.setData({
      deId: options.deId,
    })
    this.getOrderList()
    this.getDeliveryById()
  },
  getDeliveryById() {
    let params = {
      id: this.data.deId
    }
    let token = wx.getStorageSync('token')
    config._post('/deliveryman/getDeliveryById', token, params).then(res => {
      this.setData({
        canDrawMoney: res.data.canDrawMoney,
        deliveryById: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.getDeliveryById()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  toWithdrawPage() {
    wx.navigateTo({
      url: '../withdraw/index?deId=' + this.data.deId + '&canDrawMoney=' + this.data.canDrawMoney,
    })

  },
  toAccountPage() {
    wx.navigateTo({
      url: '../withdraw-detail/index?deId=' + this.data.deId,
    })
  },
  toperson() {
    wx.navigateTo({
      url: '../person/index',
    })
  },
  toaccounts() {
    wx.navigateTo({
      url: '../accounts-detail/index?deId=' + this.data.deId,
    })
  },
  todelivery() {
    wx.navigateTo({
      url: '../delivery-order/index?deId=' + this.data.deId,
    })
  }
})